/**
 * 
 */
package sqlToGUI.charts;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.LinkedList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import sqlToGUI.ChartController;

/**
 * @author Maciej
 *
 */
public class LogPanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5711831733997401666L;
	private JTextPane textPane;
	private JScrollPane scrollPane;
	private StyledDocument log;
	private String[] styles;
	
	/**A hashtable of display chart and their corresponding temporary
	 * buffers containing the most recent data this log will monitor */
	private Hashtable<GUIDisplay, LinkedList<Number[]>> chartBuffers;
	
	public LogPanel () {
		super();
		
		textPane = new JTextPane();
		textPane.setEditable(false);
		textPane.setCursor(new Cursor(Cursor.TEXT_CURSOR));

		scrollPane = new JScrollPane(textPane);
        scrollPane.setVerticalScrollBarPolicy(
        		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setWheelScrollingEnabled(true);
        
		log = textPane.getStyledDocument();
		initStyles();
		initLog();
		
		//--------------------
		addToLog("line1\n", 0);
		addToLog("line2\n", 1);
		addToLog("line3\n", 2);
		addToLog("line4\n", 3);
		addToLog("line5\n", 4);
		addToLog("line1\n", 0);
		addToLog("line2\n", 1);
		addToLog("line3\n", 2);
		addToLog("line4\n", 3);
		addToLog("line5\n", 4);
		addToLog("line1\n", 0);
		addToLog("line2\n", 1);
		addToLog("line3\n", 2);
		addToLog("line4\n", 3);
		addToLog("line5\n", 4);
		

	}
	
	private void initStyles() {
		styles = new String[] {"regular", "italic", "bold", "large", "red"};
	}
	
	private void initLog() {
		//Initialize some styles.
        Style def = StyleContext.getDefaultStyleContext().
                        getStyle(StyleContext.DEFAULT_STYLE);

        Style regular = log.addStyle("regular", def);
        StyleConstants.setFontFamily(def, "SansSerif");

        Style s = log.addStyle("italic", regular);
        StyleConstants.setItalic(s, true);

        s = log.addStyle("bold", regular);
        StyleConstants.setBold(s, true);

        s = log.addStyle("large", regular);
        StyleConstants.setFontSize(s, 14);
        
        s = log.addStyle("red", regular);
        StyleConstants.setFontSize(s, 14);
        StyleConstants.setBold(s, true);
        StyleConstants.setForeground(s, Color.red);
	}

	/**Set up the charts to where the log will listen to */
	public void listenToChartsReg(ArrayList<ChartController> controllers){

		chartBuffers = new Hashtable<GUIDisplay, LinkedList<Number[]>>();
		
		for(int i = 0; i < controllers.size(); i++){
			chartBuffers.put(controllers.get(i).registerLogToChart(this),
					new LinkedList<Number[]>());
		}
	}
	
	/**Take the next input for a given chart and send the data to the
	 * corresponding buffer where it will be monitored over */
	public void updateLog(GUIDisplay display, Number []input){
		chartBuffers.get(display).remove();
		chartBuffers.get(display).add(input);
	}
	
	/* (non-Javadoc)
	 * @see sqlToGUI.charts.GUIDisplay#addToBuffer(int, double)
	 */
	public void addToLog(String line, int severity) {
		try {
			log.insertString(log.getLength(), line, 
					log.getStyle(styles[severity]));
			textPane.setCaretPosition(log.getLength());
		} catch (BadLocationException e) {
			System.err.println("WARNING: Couldn't write to log.");
		}
	}

	/* (non-Javadoc)
	 * @see sqlToGUI.charts.GUIDisplay#getChart()
	 */
	public Container getChart() {
		return scrollPane;
	}

}
